﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace PTTK11_HuongRung
{
    public partial class frmNhapKho : Form
    {
        public frmNhapKho()
        {
            InitializeComponent();

            Timer tmr = new Timer();
            tmr.Tick += new EventHandler(timer_Tick);
            tmr.Start();
        }

        private void reLoadNguyenVatLieu()
        {
            NguyenVatLieuBUS bus = new NguyenVatLieuBUS();
            cbxTenNVL.DataSource = bus.GetNguyenVatLieu();
            cbxTenNVL.DisplayMember = "TEN_NGUYEN_VAT_LIEU";
            cbxTenNVL.ValueMember = "NGUYEN_VAT_LIEU_ID";
        }

        private void LoadDSNguyenVatLieu()
        {
            NhapKhoBUS dsBUS = new NhapKhoBUS();
            dgvDanhSach.DataSource = dsBUS.GetDSNguyenVatLieu();
        }

        private void frmNhapKho_Load(object sender, EventArgs e)
        {
            btnDangNhap.Text = "Đăng Xuất";
            NhanVienBUS nvBus = new NhanVienBUS();
            NhanVienDTO nvDto = new NhanVienDTO();
            nvDto = nvBus.GetThongTinNhanVien(Secssion.MaNV);
            txtHoTen.Text = nvDto.TENNHANVIEN.Trim();
            LoaiNhanVienBUS lnvbus = new LoaiNhanVienBUS();
            txtMaNV.Text = lnvbus.GetTenLoaiNV(nvDto.LOAINHANVIEN);
            reLoadNguyenVatLieu();
            LoadDSNguyenVatLieu();

            dgvDanhSach.Columns[0].HeaderText = "ID NVL";
            dgvDanhSach.Columns[1].HeaderText = "Tên NVL";
            dgvDanhSach.Columns[2].HeaderText = "Loại NVL";
            dgvDanhSach.Columns[3].HeaderText = "Số lượng";
            dgvDanhSach.Columns[4].HeaderText = "Đơn vị";
        }

        private void btnNhapKho_Click(object sender, EventArgs e)
        {
            NhapKhoBUS nkBUS = new NhapKhoBUS();
            NhapKhoDTO nkDTO = new NhapKhoDTO();

            string ID = cbxTenNVL.SelectedValue.ToString().Trim();

            int nvlid = nkBUS.GetDonHangID(ID);

            nkDTO.SoLuong = int.Parse(txtSoLuong.Text);
            nkDTO.NvlID = nvlid;

            nkBUS.NhapKho(nkDTO);

            MessageBox.Show("Nhập kho thành công");
            txtSoLuong.Clear();
            LoadDSNguyenVatLieu();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmQuanLy frm = new frmQuanLy();
            frm.ShowDialog();
            this.Close();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            ThoiGian.Text = DateTime.Now.ToString("dd/MM/yy HH:mm:ss");
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (btnDangNhap.Text.Trim().CompareTo("Đăng Xuất") == 0)
            {
                btnDangNhap.Text = "Đăng Nhâp";
                Secssion.MaNV = "";
                Secssion.MaKhac = "";

                txtHoTen.Text = "";
                txtMaNV.Text = "";
                this.Hide();
                frmQuanLy frm = new frmQuanLy();
                frm.ShowDialog();
                this.Close();
            }
            else
            {
                this.Hide();
                frmDangNhap frm = new frmDangNhap();
                frm.ShowDialog();
                this.Close();
            }
        }
    }
}
